const query = require('../../dbConfig/index')
const { generateToken } = require('../../utils/middleware')
const login = async (ctx,next)=>{
	const body = ctx.request.body
	let res = null;
	if(body.cardid==1){
		res = await query(`select * from user where username='${body.username}' and password='${body.password}' and power='${body.cardid}'`)
	}else{
		res = await query(`select * from teacher where teacher_username='${body.username}' and teacher_password='${body.password}'`)
		console.log(res)
		res.length>0 && (res[0].username = res[0].teacher_username)
	}
	if(res.length===1){
		console.log(res)
		res[0].token = generateToken(res[0].username,res[0].power,res[0].classes)
		ctx.body={code:200,data:{...res[0]},msg:'成功'}
		next()
	}else{
		ctx.body={code:0,data:null,msg:'用户名或密码错误'}
		return
	}
}
module.exports = {
	login
}